
// pages/SCM/maintain/maintain.js
const app = getApp()
const util = require('../../../utils/util.js')
const lbbService = require('../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    membersName: [],
    pileChairListNew:[],
    feedbackNew:[],
    noInspectedNew: [],
    masterUid:0,
    manIndex: 0,
    pageNum:1,
    pageSize:20,
    scrollShow:true,
    chairPileStatus: [
      { value: '1', name: '低电桩', checked: true, key:'lowPower' },
      { value: '2', name: '维护中', checked: false, key:'maintain' },
      { value: '3', name: '待升级', checked: false, key:'upgrade' },
      { value: '4', name: '蓝牙连不上', checked: false, key:'noBlueCount' },
      { value: '5', name: '归还rfid不识别', checked: false, key:'noReadRFIDCount' },
      { value: '43', name: '租赁rfid不识别', checked: false, key:'leaseNoReadRFIDCount' },
      { value: '42', name: '待更换抗铁rfid', checked: false, key:'noNewRfid' },
      // { value: '8', name: '欧姆龙开关', checked: false, key:'omron' },
      // { value: '9', name: '轻弹簧', checked: false, key:'light_spring_steel_plate' },
      { value: '10', name: '弹不开', checked: false, key:'noEjectCount' },
      // { value: '11', name: '复位电容', checked: false, key:'reset' },
      { value: '13', name: '搜索超时', checked: false, key:'blueSearchTimeout' },
      { value: '20', name: '客服反馈问题', checked: false, key:'feedbackCount' },
      { value: '40', name: '超40天未巡检', checked: false, key:'noInspectedCount' },
      { value: '14', name: '重启3次以上', checked: false, key:'powerOnGteater3' },
      //{ value: '6', name: '插入开关', checked: false, key:'switchNoCount' },
      //{ value: '7', name: '锁钩开关', checked: false, key:'hookStatNoCount' },
      { value: '15', name: '新插入开关', checked: false, key:'newSwitchNoCount' },
      { value: '16', name: '旧插入开关', checked: false, key:'oldSwitchNoCount' },
      { value: '17', name: '新锁钩开关', checked: false, key:'newHookStatNoCount' },
      { value: '18', name: '旧锁钩开关', checked: false, key:'oldHookStatNoCount' },
      { value: '12', name: '丢轮椅控制盒', checked: false, key:'losedChairIds' },
      { value: '41', name: '待更换锂电池', checked: false, key:'notReplaceBatteryCount' },
    ],
    chairPileStatusValue: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.setData({
      scrollHeight: app.getScrollHeight(375)
    })
    this.getMembers()
    this.getPageInspectPile()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    let that = this
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getMembers: function () {
    let that = this
    lbbService.getMembers({roleId:3}, function (res) {
      that.setData({
        members: res.data
      })
      that.setMembers()
    })
  },
  setMembers: function () {
    let that = this
    let members = that.data.members
    let membersName = that.data.membersName
    members.forEach(item => {
      membersName.push(item.masterName)
    })
    that.setData({
      membersName: membersName
    })
    that.getChairPileStatusCount()
  },
  getChairPileStatusCount:function(){
    let that = this
    lbbService.getInspectAllPileCount({ userId: that.data.masterUid }, function (res) {
      //第一次页面加载查询默认查询的userId不确定，由后台返回哪个userId
      if (that.data.masterUid == 0) {
        let members = that.data.members
        let manIndex = 0;
        let index = 0;
        members.forEach(item => {
          if (item.masterUid == res.userId) {
            manIndex = index;
          }
          index++;
        })
        that.setData({
          masterUid: res.userId,
          manIndex: manIndex,
        })
      }
      that.setChairPileStatusCount(res)
    })
  },
  manChange: function (e) {
    let that = this
    let index = e.detail.value
    if(that.data.masterUid==0){
      return false;
    }
    that.setData({
      manIndex: index,
      scrollShow: false
    })
    that.setData({
      masterUid: that.data.members[that.data.manIndex].masterUid,
      pageNum:1,
      // chairPileStatusValue:1
    })
    that.getChairPileStatusCount()
    that.getPageInspectPile()
  },
  getPageInspectPile: function () {
    let that = this
    let queryParam = { userId: that.data.masterUid, errType: that.data.chairPileStatusValue,
           pageNum: that.data.pageNum, pageSize:that.data.pageSize}
    lbbService.getPageInspectPile(queryParam, function (res) {
      if (that.data.pageNum > 1 && res.length==0){
        wx.showToast({
          title: '数据加载完毕',
          duration: 2000,
        })
      }
      if (that.data.chairPileStatusValue==20){//客服反馈
        if (that.data.pageNum == 1) {
          that.setData({
            feedbackNew: res,
          }) 
        } else {
          that.setData({
            feedbackNew: that.data.feedbackNew.concat(res),
          })
        }
      } else if (that.data.chairPileStatusValue==40){//未巡检的医院
        if (that.data.pageNum == 1) {
          that.setData({
            noInspectedNew: res,
          })
        } else {
          that.setData({
            noInspectedNew: that.data.noInspectedNew.concat(res),
          })
        }
      }else if (that.data.chairPileStatusValue==42){//待更换抗铁rfid
        let chairListNew = that.data.chairListNew;
        let newArr = new Array();
        newArr = res.concat([]);
        if (that.data.pageNum == 1) {
          chairListNew = res;
        } else {//当下次加载的桩位与上次加载的桩位医院相同 ，则合并
          let lastHospitalPile = chairListNew[chairListNew.length - 1]
          let firstHospitlalPile = newArr.shift()
          if (lastHospitalPile && firstHospitlalPile && lastHospitalPile.equHospitalName == firstHospitlalPile.equHospitalName) {
            lastHospitalPile.chairList = lastHospitalPile.chairList.concat(firstHospitlalPile.chairList)
          } else if (firstHospitlalPile) {
            newArr.unshift(firstHospitlalPile)
          } 
          chairListNew.splice(chairListNew.length - 1, 1, lastHospitalPile)
          chairListNew = that.data.chairListNew.concat(newArr)
        }
        that.setData({
          chairListNew: chairListNew,
        }) 
      }else {//巡检的桩
        let pileChairListNew = that.data.pileChairListNew;
        let newArr = new Array();
        newArr = res.concat([]);
        if (that.data.pageNum == 1) {
          pileChairListNew = res;
        } else {//当下次加载的桩位与上次加载的桩位医院相同 ，则合并
          let lastHospitalPile = pileChairListNew[pileChairListNew.length - 1]
          let firstHospitlalPile = newArr.shift()
          if (lastHospitalPile && firstHospitlalPile && lastHospitalPile.hospitalName == firstHospitlalPile.hospitalName) {
            lastHospitalPile.pileListList = lastHospitalPile.pileListList.concat(firstHospitlalPile.pileListList)
          } else if (firstHospitlalPile) {
            newArr.unshift(firstHospitlalPile)
          } 
          pileChairListNew.splice(pileChairListNew.length - 1, 1, lastHospitalPile)
          pileChairListNew = that.data.pileChairListNew.concat(newArr)
        }
        that.setData({
          pileChairListNew: pileChairListNew,
        }) 
      }
      
      that.setData({
        scrollShow: true,
      }) 
    })
  },
 
  // 轮椅桩筛选
  chairPileStatusChange: function (e) {
    let that = this
    console.log("pileChairChange-->", e);
    let value = parseInt(e.currentTarget.dataset.value);
    let pileStatusValue = that.data.chairPileStatusValue;
    console.log("pileStatusValue-->", pileStatusValue, value);
    if (value == pileStatusValue) {
       that.clearRadio(value)
    } else {
      //tempFilter[i].pileListList = pileList[i].pileListList.filter(item => item.lowPower == true)
      that.setRadio(value)
    }
  },
  setRadio: function ( value) {
    let that = this
    let chairPileStatus = that.data.chairPileStatus
    chairPileStatus.forEach(item => {
      if (item.value == value) {
        item.checked = true
      } else {
        item.checked = false
      }
    })
    that.setData({
      pileChairListNew:[],
      feedbackNew:[],
      noInspectedNew:[],
      chairPileStatus: chairPileStatus,
      chairPileStatusValue: value,
      pageNum:1,
      scrollShow:false
    })
    that.getPageInspectPile()
  },
  clearRadio: function (value) {
    let that = this
    let chairPileStatus = that.data.chairPileStatus
    if (value == 20 || value==40){
      return false;
    }
    chairPileStatus.forEach(item => {
      item.checked = false
    })
    that.setData({
      chairPileStatus: chairPileStatus,
      chairPileStatusValue: 0,
      pageNum:1
    })
    that.getPageInspectPile()
  },

  setChairPileStatusCount: function (res) {
    let that=this
    // that.data.chairPileStatus.forEach(item=>{
    //   item.checked=false
    // })
    that.data.chairPileStatus.forEach(item=>{
      Object.keys(res).forEach(key=>{
        if(item.key==key){
          item.num=res[key];
        }
      })
    })
    

    that.setData({
      chairPileStatus: that.data.chairPileStatus
    })
    
    
  },
  getMore: function () {
    let that = this
    that.data.pageNum += 1
    that.getPageInspectPile()
  },


})